package com.zg.scf.web;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zg.common.core.R;
import com.zg.common.core.dao.scf.EnLevelDao;
import com.zg.common.core.dao.scf.entity.EnLevel;
import com.zg.scf.domain.req.EnLevelReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = {"企业等级管理"})
@RestController
@RequiredArgsConstructor
public class EnLevelController {

    private final EnLevelDao enLevelDao;

    @PostMapping("/enLevel/list")
    @ApiOperation(value = "查询")
    public R<List<EnLevel>> list(@RequestBody EnLevelReq req) {
        List<EnLevel> list = enLevelDao.list(
                new LambdaQueryWrapper<EnLevel>()
                        .eq(StrUtil.isNotBlank(req.getEnType()), EnLevel::getEnType, req.getEnType())
                .orderByAsc(EnLevel::getSort)
        );
        return R.success(list);
    }


    @PostMapping("/enLevel/remove")
    @ApiOperation(value = "删除")
    public R remove(@RequestBody List<Long> idList) {
        enLevelDao.removeBatchByIds(idList);
        return R.success();
    }

    @PostMapping("/enLevel/addOrUpdate")
    @ApiOperation(value = "新增或修改")
    @Transactional(rollbackFor = Exception.class)
    public R addOrUpdate(@RequestBody EnLevel req) {
        enLevelDao.saveOrUpdate(req);
        return R.success();
    }
}
